/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: TxRecv
 * Author:   cakin
 * Date:     2020/4/25
 * Description:消费者
 */
package com.rabbitmq.tx;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.util.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName: TxRecv
 * @Description: 消费者
 * @Date: 2020/4/25
 * @Author: cakin
 */
@Slf4j
public class TxRecv {
    /**
     * 队列名
     */
    private static final String QUEUE_NAME = "test_queue_tx";

    public static void main( String[] args ) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionUtils.getConnection();

        // 打开通道
        Channel channel = connection.createChannel();

        // 申明要消费的队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 消费消息
        channel.basicConsume(QUEUE_NAME, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery( String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body ) throws IOException {
                // 接收到的消息
                String message = new String(body);
                log.info(" [x] Received '" + message + "'");
            }
        });
    }
}

